package com.jcms.cms.dao.parcel.impl;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.jcms.cms.dao.parcel.OrderDao;
import com.jcms.cms.entity.parcel.Order;
import com.jcms.common.hibernate3.Finder;
import com.jcms.common.hibernate3.HibernateBaseDao;
import com.jcms.common.page.Pagination;

@Repository
public class OrderDaoImpl extends HibernateBaseDao<Order, Integer> implements OrderDao {

	@Override
	protected Class<Order> getEntityClass() {
		return Order.class;
	}

	public int save(Order order) {
		int num = (Integer) getSession().save(order);
		int id = order.getId();
		if(String.valueOf(id).length() < 5) {
			StringBuilder sb = new StringBuilder();
			for(int i=0; i<5-String.valueOf(id).length(); i++) {
				sb.append(0);
			}
			order.setOrderNumber(sb.toString() + id);
		} else {
			order.setOrderNumber(String.valueOf(id));
		}
		return num;
	}

	@SuppressWarnings("unchecked")
	public List<Order> getOrdersByUser(Integer id) {
		return createQuery("from Order where user.id=?", id).list();
	}

	public Pagination getPage(int pageNo, int pageSize) {
		Finder f = Finder.create("from Order order by id desc");
		return find(f, pageNo, pageSize);
	}

	public Order getOrderById(Integer id) {
		return get(id);
	}

	public void updateOrder(Order order) {
		getSession().update(order);
	}
	
}
